﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TestWebService
{
    class Program
    {
        static void Main(string[] args)
        {
            // Instanciation of the TransfertImageClient service reference
            //
            ImageTransfertServiceReference.TransfertImageClient imageTransfertService = new ImageTransfertServiceReference.TransfertImageClient();

            // Build upload request
            //
            ImageTransfertServiceReference.ImageInfo imageInfo = new ImageTransfertServiceReference.ImageInfo();
            imageInfo.IDAlbum = 1;
            MemoryStream imageStream = new MemoryStream(lireFichier(@"C:\Users\user\Pictures\NY.jpg"));
            //ImageTransfertServiceReference.ImageUploadRequest request = new ImageTransfertServiceReference.ImageUploadRequest(imageInfo, imageStream);

            // Call web method
            //
            imageTransfertService.uploadImage(imageInfo, imageStream);
            Console.Out.WriteLine("Transfert Terminé");
            Console.ReadLine();

            // Download an image
            //
            imageInfo.ID = 1;
            Stream image = imageTransfertService.downloadImage(imageInfo);
            Console.WriteLine("Download terminé");
            Console.ReadLine();
        }

        /// <summary>
        /// Lit et retourne le contenu du fichier sous la forme de tableau de byte
        /// </summary>
        /// <param name="chemin">chemin du fichier</param>
        /// <returns></returns>
        private static byte[] lireFichier(string chemin)
        {
            byte[] data = null;
            FileInfo fileInfo = new FileInfo(chemin);
            int nbBytes = (int)fileInfo.Length;
            FileStream fileStream = new FileStream(chemin, FileMode.Open,
            FileAccess.Read);
            BinaryReader br = new BinaryReader(fileStream);
            data = br.ReadBytes(nbBytes);
            return data;
        }
    }
}
